import re


def analyse(status, line):
    if status == "generate_build_system_bar":
        return analyse_in_generate_build_system(line)
    elif status == "cmake_config_bar":
        return analyse_in_cmake_config(line)
    elif status == "devicetree_config_bar":
        return analyse_in_devicetree_config(line)
    elif status == "kconfig_process_bar":
        return analyse_in_kconfig_process(line)
    elif status == "compilers_recognize_bar":
        return analyse_in_compilers_recognize(line)
    elif status == "buildapplication_bar":
        return analyse_in_buildapplication(line)
    return None, None
    
def analyse_is_finished(line):
    if "Memory region" in line:
        return True
    return False

def analyse_is_error(line):
    if "Error" in line:
        return True
    elif "error" in line:
        return True
    elif "failed" in line:
        return True
    elif "Failed" in line:
        return True
    elif "not found" in line:
        return True
    elif "ERROR" in line:
        return True
    elif "error" in line:
        return True
    elif "FAIL" in line:
        return True
    elif "undefined" in line:
        return True
    return False

def analyse_is_warning(line):
    if "Warning" in line:
        return True
    elif "warning" in line:
        return True
    return False

def analyse_is_key_error(line):
    if "No such file" in line:
        return True
    elif "unknown type name" in line:
        return True
    elif "undeclared" in line:
        return True
    elif "undefined" in line:
        return True
    return False

def analyse_mem_usage(line):
    usage_pattern = re.compile(r'(\d+\.\d+)%')
    match = usage_pattern.search(line)
    if match:
        usage_rate = float(match.group(1))

        if "FLASH:" in line:
            return "flash_use_bar", usage_rate
        elif "RAM:" in line:
            return "ram_use_bar", usage_rate
        elif "IDT_LIST:" in line:
            return "idt_use_bar", usage_rate
    return None, None

def analyse_in_generate_build_system(line):
    if "west build: making build dir" in line:
        return "generate_build_system_bar", 40
    if "generating a build system" in line:
        return "generate_build_system_bar", 70
    if "Loading Zephyr default modules" in line:
        return "generate_build_system_bar", 100
    return None, None


def analyse_in_cmake_config(line):
    if "-- CMake version:" in line:
        return "cmake_config_bar", 10
    if "-- Found Python3:" in line:
        return "cmake_config_bar", 20
    if "-- Cache files will be written to:" in line:
        return "cmake_config_bar", 30
    if "-- Zephyr version:" in line:
        return "cmake_config_bar", 40
    if "-- Found west" in line:
        return "cmake_config_bar", 50
    if "-- Board:" in line:
        return "cmake_config_bar", 60
    if "-- Found host-tools:" in line:
        return "cmake_config_bar", 70
    if "-- Found toolchain:" in line:
        return "cmake_config_bar", 80
    if "-- Found Dtc:" in line:
        return "cmake_config_bar", 100
    return None, None



def analyse_in_devicetree_config(line):
    if "-- Found BOARD.dts:" in line:
        return "devicetree_config_bar", 10
    if "-- Generated zephyr.dts:" in line:
        return "devicetree_config_bar", 30
    if "-- Generated devicetree_generated.h:" in line:
        return "devicetree_config_bar", 60
    if "-- Including generated dts.cmake file:" in line:
        return "devicetree_config_bar", 80
    if "Parsing" in line:
        return "devicetree_config_bar", 100
    return None, None

def analyse_in_kconfig_process(line):
    if "Loaded configuration" in line:
        return "kconfig_process_bar", 10
    if "Merged configuration" in line:
        return "kconfig_process_bar", 30
    if "Configuration saved to" in line:
        return "kconfig_process_bar", 60
    if "Kconfig header saved to" in line:
        return "kconfig_process_bar", 80
    if "-- Found GnuLd:" in line:
        return "kconfig_process_bar", 100
    return None, None


def analyse_in_compilers_recognize(line):
    if "-- Found GnuLd:" in line:
        return "compilers_recognize_bar", 10
    if "-- The C compiler identification is GNU" in line:
        return "compilers_recognize_bar", 20
    if "-- The CXX compiler identification is GNU" in line:
        return "compilers_recognize_bar", 80
    if "-- The ASM compiler identification is GNU" in line:
        return "compilers_recognize_bar", 90
    if "-- Found assembler:" in line:
        return "compilers_recognize_bar", 100
    return None, None
   
def analyse_in_buildapplication(line):
    if "[" in line:
        match = re.search(r"\[(\d+)/(\d+)\]", line)
        if match:
            persantage = int(match.group(1)) / int(match.group(2)) * 100
            return "buildapplication_bar", persantage    
    return None, None   

def analyse_demo(log_file):
    with open(log_file, 'r') as file:
        for line in file:
            if "Application: " in line:
                app_path = line.split("Application: ")[1].strip()
                print(app_path)
            if "CMake version: " in line:
                cmake_version = line.split("CMake version: ")[1].strip()
                print(cmake_version)
            if "Found Python3" in line:
                match = re.search(r'found suitable version "(\d+\.\d+\.\d+)"', line)
                python_version = match.group(1)
                print(python_version)
            if "Zephyr version: " in line:
                match = re.search(r'Zephyr version: (\d+\.\d+\.\d+)', line)
                zephyr_version = match.group(1)
                print(zephyr_version)
            if "Found west" in line:
                match = re.search(r'found suitable version "(\d+\.\d+\.\d+)"', line)
                west_version = match.group(1)
                print(west_version)
            if "Board: " in line:
                board = line.split("Board: ")[1].strip()
                print(board)
            if "Found host-tools:" in line:
                match = re.search(r'(\d+\.\d+\.\d+)', line)
                host_tools_version = match.group(0)
                print(host_tools_version)
            if "Found toolchain:" in line:
                match = re.search(r'(\d+\.\d+\.\d+)', line)
                toolchain_version = match.group(0)
                print(toolchain_version)
            if "Found Dtc:" in line:
                match = re.search(r'(\d+\.\d+\.\d+)', line)
                dtc_version = match.group(0)
                print(dtc_version)
            if "Found BOARD.dts:" in line:
                dts_file = line.split("Found BOARD.dts: ")[1].strip()
                print(dts_file)
            if "Generated zephyr.dts:" in line:
                zephyr_dts_file = line.split("Generated zephyr.dts: ")[1].strip()
                print(zephyr_dts_file)
            if "Generated devicetree_generated.h:" in line:
                devicetree_generated_h = line.split("Generated devicetree_generated.h: ")[1].strip()
                print(devicetree_generated_h)
            if "Including generated dts.cmake file:" in line:
                dts_cmake_file = line.split("Including generated dts.cmake file: ")[1].strip()
                print(dts_cmake_file)
            if "Loaded configuration" in line:
                match = re.search(r"Loaded configuration '(.+)'", line)
                loaded_configuration = match.group(1)
                print(loaded_configuration)
            if "Merged configuration" in line:
                match = re.search(r"Merged configuration '(.+)'", line)
                merged_configuration = match.group(1)
                print(merged_configuration)
            if "Configuration saved to" in line:
                match = re.search(r"Configuration saved to '(.+)'", line)
                configuration_saved = match.group(1)
                print(configuration_saved)
            if "Kconfig header saved to" in line:
                match = re.search(r"Kconfig header saved to '(.+)'", line)
                kconfig_header_saved = match.group(1)
                print(kconfig_header_saved)
            if "[" in line:
                match = re.search(r"\[(\d+)/(\d+)\]", line)
                if match:
                    persantage = int(match.group(1)) / int(match.group(2)) * 100
                    print(persantage)
    